package com.muniuliuma.basecontrol;

import java.util.Arrays;
import java.util.List;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.DialogPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class BasePreferenceActivity extends PreferenceActivity {
	
	private	PreferenceScreen mRoot;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MenuControl.sCurrentActivity = this;
		ActivityStack.getInstance().pushActivity(this);
		mRoot = getPreferenceManager().createPreferenceScreen(this);
		setPreferenceScreen(mRoot);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MenuControl.sCurrentActivity = this;
		unlockScreen();
	}
	
	@Override
	public void finish() {
		super.finish();
		ActivityStack.getInstance().popActivity(this);
	}

	
	protected PreferenceScreen getRootPreference(){
		return mRoot;
	}
	
	public PreferenceScreen addPreferenceScreen(int StingID){
		return addPreferenceScreen(StingID, -1);
	}
	
	public PreferenceScreen addPreferenceScreen(int StingID, int summaryResId){
		PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(this);
		preferenceScreen.setTitle(StingID);
		if(summaryResId>0){
			preferenceScreen.setSummary(summaryResId);
		}
		mRoot.addPreference(preferenceScreen);
		
		return preferenceScreen;
	}
	
	public CheckBoxPreference addCheckBoxPreference(int titleID, SharedPreferenceData key, boolean checked){
		return addCheckBoxPreference(titleID, -1, -1, -1, key, checked);
	}
	
	public CheckBoxPreference addCheckBoxPreference(int titleID, int summaryResId, SharedPreferenceData key, boolean checked){
		return addCheckBoxPreference(titleID, summaryResId, -1, -1, key, checked);
	}
	
	public CheckBoxPreference addCheckBoxPreference(int titleID, int summaryResId, int summaryOnResId, int summaryOffResId, SharedPreferenceData key, boolean checked){
		CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);
		checkBoxPreference.setTitle(titleID);
		
		if(key!=null){
			boolean isChecked = SharedPreferenceData.getBoolean(key, checked);
			checkBoxPreference.setDefaultValue(isChecked);
		} else {
			checkBoxPreference.setDefaultValue(checked);
		}
		if(summaryResId>0){
			checkBoxPreference.setSummary(summaryResId);
		}
		if(summaryOnResId>0){
			checkBoxPreference.setSummaryOn(summaryOnResId);
		}
		if(summaryOffResId>0){
			checkBoxPreference.setSummaryOff(summaryOffResId);
		}
		mRoot.addPreference(checkBoxPreference);
		return checkBoxPreference;
	}
	
	public ListPreference addListPreference(int titleID, int entries, int entryValues, SharedPreferenceData key, int summaryID){
		ListPreference listPreference = new ListPreference(this);
		listPreference.setTitle(titleID);
		listPreference.setDialogTitle(titleID);
		listPreference.setEntries(entries);
		listPreference.setEntryValues(entryValues);
		
		List<String> entriesList = Arrays.asList(getResources().getStringArray(entries));
		List<String> entryValuesList = Arrays.asList(getResources().getStringArray(entryValues));
		String value = entryValuesList.get(entriesList.indexOf(getString(summaryID)));
		
		if(key!=null){
			value = SharedPreferenceData.getString(key, value);
		}
		listPreference.setSummary(entriesList.get(entryValuesList.indexOf(value)));
		listPreference.setValue(value);
		mRoot.addPreference(listPreference);
		return listPreference;
	}
	
	protected String getEntry(String entryValue, int entries, int entryValues){
		List<String> entriesList = Arrays.asList(getResources().getStringArray(entries));
		List<String> entryValuesList = Arrays.asList(getResources().getStringArray(entryValues));
		return entriesList.get(entryValuesList.indexOf(entryValue));
	}

	protected String getEntryValue(String entry, int entries, int entryValues){
		List<String> entriesList = Arrays.asList(getResources().getStringArray(entries));
		List<String> entryValuesList = Arrays.asList(getResources().getStringArray(entryValues));
		return entryValuesList.get(entriesList.indexOf(entry));
	}
	
	protected int getIndexOfEntry(String entry, int entries){
		List<String> entriesList = Arrays.asList(getResources().getStringArray(entries));
		return entriesList.indexOf(entry);
	}

	protected int getIndexOfEntryValue(String entryValue, int entryValues){
		List<String> entriesList = Arrays.asList(getResources().getStringArray(entryValues));
		return entriesList.indexOf(entryValue);
	}
	
	public DialogPreference addDialogPreference(int titleId, int dialogTitleId, int dialogMsgId){
		IDialogPreference dialogPref = new IDialogPreference(this);
		dialogPref.setTitle(titleId);
		dialogPref.setDialogTitle(dialogTitleId);
		dialogPref.setDialogMessage(dialogMsgId);
		mRoot.addPreference(dialogPref);
		return dialogPref;
	}
	
	public void setDlgPrefPositiveBtn(DialogPreference diaPref, int stringId,
			DialogInterface.OnClickListener onClick) {
		((IDialogPreference) diaPref).setPositeButton(stringId, onClick);
	}
	
	public EditTextPreference addEditTextPreference(int titleId, int dialogTitle, int dialogMsg, SharedPreferenceData key){
		EditTextPreference editPref = new EditTextPreference(this);
		editPref.setTitle(titleId);
		String defaultValue = SharedPreferenceData.getString(key);
		editPref.setSummary(defaultValue);
		editPref.setDefaultValue(defaultValue);
		editPref.setDialogTitle(dialogTitle);
		editPref.setDialogMessage(dialogMsg);
		mRoot.addPreference(editPref);
		return editPref;
	}
	


	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (MenuControl.isWinscapeLocked()) {
			return true;
		} else {
			return super.dispatchKeyEvent(event);
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (MenuControl.isWinscapeLocked()) {
			return true;
		} else {
			return super.dispatchTouchEvent(ev);
		}
	}

	@Override
	public boolean dispatchTrackballEvent(MotionEvent ev) {
		if (MenuControl.isWinscapeLocked()) {
			return true;
		} else {
			return super.dispatchTrackballEvent(ev);
		}
	}
	
	public void startActivity(Class<? extends Activity> cls) {
		MenuControl.changeWinscape(this, cls);
	}

	public void backActivity() {
		MenuControl.backward(this);
	}

	public void lockScreen() {
		MenuControl.lockWinscape(true);
	}

	public void unlockScreen() {
		MenuControl.lockWinscape(false);
	}
	
	public boolean isWinscapeLocked(){
		return MenuControl.isWinscapeLocked();
	}

	private class IDialogPreference extends DialogPreference{
		
		private int positiveText;
		private DialogInterface.OnClickListener onCliskPositiveButton;

		public IDialogPreference(Context context) {
			super(context, null);
		}
		public void setPositeButton(int positiveText, DialogInterface.OnClickListener onCliskPositiveButton){
			this.positiveText = positiveText;
			this.onCliskPositiveButton = onCliskPositiveButton;
		}
		protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
			if(onCliskPositiveButton != null ){
				builder.setPositiveButton(positiveText, onCliskPositiveButton);
			}
	    }
	}

}
